package com.gowants.pems.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gowants.pems.aspect.Importance;
import com.gowants.pems.aspect.OperType;
import com.gowants.pems.aspect.ScmsAopLog;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.model.SysPower;
import com.gowants.pems.model.SysRole;
import com.gowants.pems.service.SysPowerService;
import com.gowants.pems.service.SysRoleService;
import com.gowants.pems.util.JSONList;
import com.gowants.pems.util.JSONMap;
@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController
{
    @Autowired
    private SysRoleService scmsRoleService;
	@Autowired
	private SysPowerService scmsPowerService;
    /**
     * <列表分页条件查询>
     * @param page
     * @param limit
     * @param reqData
     * @param req
     * @return
     * @remake create by TPH 2020年9月2日 上午10:15:01
     */
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
    @ScmsAopLog(description = "角色列表查看", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap getList(@RequestParam(defaultValue = "1", required = false, value = "page") int page,
            @RequestParam(defaultValue = "10", required = false, value = "limit") int limit,
            @RequestParam(defaultValue = "", required = false, value = "reqData") String reqData,
            HttpServletRequest req) 
    {
        try
        {
        	if (!isPower("/sysRole/getList.do")) {
				return doResponse(req, "TSMS_0000228");
			}
            JSONMap resultMap = new JSONMap();
            SysRole record = new SysRole();
            if (!"".equals(reqData)) 
            {
                JSONList jsonCondition = getCondition(reqData);

                for (Object cond : jsonCondition) {
                    this.buildCondition(cond, record);
                }
            }
            // 设置分页查询信息
            PageHelper.startPage(page, limit);
            List<SysRole> list = scmsRoleService.select(record);
            PageInfo<SysRole> pageInfo = new PageInfo<SysRole>(list);
            // 赋值方法
            pageSetData(req, resultMap, page,  limit,pageInfo.getPages(), "", list, pageInfo.getTotal());

            return doResponse(req, "", resultMap);
        }
        catch (Exception e)
        {
            return doResponse(req, e.getMessage(), null);
        }
       
    }
    @RequestMapping(value = "/selectList")
    @AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
    public JSONMap selectList(String request, HttpServletRequest req) 
    {
    	 SysRole record = new SysRole();
    	 if(StringUtils.isNotEmpty(request))
    	 {
    		 record=getRequestBody(request,SysRole.class);
    	 }
    	 List<SysRole> list = scmsRoleService.select(record);
    	 return doResponse(req, "", list);
    }
    
    
    /**
     * <保存>
     * @param request
     * @param req
     * @return
     * @remake create by TPH 2020年9月2日 上午10:15:24
     */
    @RequestMapping(value = "/save")
    @AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
    @ScmsAopLog(description = "角色添加", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap save(String request, HttpServletRequest req) 
    {
        try
        {
        	if (!isPower("/sysRole/save.do")) {
				return doResponse(req, "TSMS_0000228");
			}
        	JSONMap requestBody = getRequestBody(request);
            SysRole record = getRequestBody(requestBody.getString("scmsUser"),SysRole.class);
            String scmsPower = requestBody.getString("scmsPower");
			JSONList scmsPowerList = JSONList.parseJSONList(scmsPower);
            String code = scmsRoleService.saveNew(record,getUserId(),scmsPowerList);
            return doResponse(req, code, null);
        }
        catch (Exception e)
        {
            return doResponse(req, e.getMessage(), null);
        }
    }
    
    /**
     * <编辑>
     * @param request
     * @param req
     * @return
     * @remake create by TPH 2020年9月2日 上午10:15:41
     */
    @RequestMapping(value = "/update")
    @AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
    @ScmsAopLog(description = "角色编辑", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap update(String request, HttpServletRequest req) 
    {
        try
        {
        	if (!isPower("/sysRole/update.do")) {
				return doResponse(req, "TSMS_0000228");
			}
        	JSONMap requestBody = getRequestBody(request);
            SysRole record = getRequestBody(requestBody.getString("scmsUser"),SysRole.class);
            String scmsPower = requestBody.getString("scmsPower");
			JSONList scmsPowerList = JSONList.parseJSONList(scmsPower);
            //执行编辑操作
            String code = scmsRoleService.updateNew(record,getUserId(),scmsPowerList);
            return doResponse(req, code, null);
        }
        catch (Exception e)
        {
            return doResponse(req, e.getMessage(), null);
        }
    }
    
    /**
     * <删除>
     * @param request
     * @param req
     * @return
     * @remake create by TPH 2020年9月2日 上午10:15:52
     */
    @RequestMapping(value = "/delete")
    @AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
    @ScmsAopLog(description = "角色删除", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap delete(String request, HttpServletRequest req) 
    {
        String code = "TSMS_0000048";//信息删除失败
        try
        {
        	if (!isPower("/sysRole/delete.do")) {
				return doResponse(req, "TSMS_0000228");
			}
            JSONMap requestBody = getRequestBody(request);
            String id = requestBody.getString("id");
            int exeNum = scmsRoleService.deleteAll(id);
            if(exeNum > 0) 
            {
                code = "TSMS_0000050";//删除成功
            }
        }
        catch (Exception e)
        {
            code = e.getMessage();
        }
        return doResponse(req, code, null);
    }
    
   
    /**
     * <主键id查询>
     * @param request
     * @param req
     * @return
     * @remake create by TPH 2020年9月2日 上午10:16:05
     */
    @RequestMapping(value = "/selectOne")
    @AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
    @ScmsAopLog(description = "角色信息单条查询", opType = OperType.execute, importance = Importance.commonly)
    public JSONMap selectOne(String request, HttpServletRequest req) 
    {
        String code = "";
        try 
        {
        	JSONMap resultMap = new JSONMap();
            JSONMap requestBody = getRequestBody(request);
            String id = requestBody.getString("id");
            if ("".equals(id) || null == id) {
                code = "TSMS_0000205";// 请输入id
                return doResponse(req, code, null);

            }
            SysRole record = scmsRoleService.selectOne(id);
            resultMap.put("sysRole", record);
            SysPower fp = new SysPower();
            fp.setRoleId(id);
			fp.setIsenable(1);
			List<SysPower> scmsPower = scmsPowerService.select(fp);
			resultMap.put("sysPower", scmsPower);
            return doResponse(req, code, resultMap);
        } 
        catch (Exception e) 
        {
            code = e.getMessage();
        }
        return doResponse(req, code, null);
    }
    
    /**
     * <构建查询条件>
     * @param cond
     * @param record
     * @remake create by TPH 2020年9月2日 上午10:16:29
     */
    private void buildCondition(Object cond, SysRole record) 
    {
        JSONMap condition = JSONMap.parseJSONMap(cond.toString());

        String field = condition.getString("field");
        String keyword = condition.getString("keyword");

        if (StringUtils.isNotEmpty(field) && StringUtils.isNotEmpty(keyword)) 
        {
            switch (field) 
            {
                case "id":
                    record.setId(keyword);
                    break;
                case "name":
                    record.setName(keyword);
                    break;
                case "remarks":
                    record.setRemarks(keyword);
                    break;
                case "createUname":
                    record.setCreateUname(keyword);
                    break;
            }
        }
    }
}
